<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<div id="app">
  <cpn></cpn>
  <cpn></cpn>
  <cpn ref="aaa"></cpn> <!-- $refs 引用模版的时候必须有 ref 标签,之后根据这个标签去找值 -->
  <button @click="butClick">按钮</button>
</div>
</body>
<template id="cpn">
  <div>我是子组件</div>
</template>
<script src="../js/vue.js"></script>
<script>

  let app = new Vue({
    el: "#app",
    data: {
      message: "aaa",
    },
    methods: {
      butClick() {
        // 1.$children 获取所有子组件属性
        // console.log(this.$children);
        // for (let c of this.$children) {
        //   console.log(c.name);
        //   c.showMessage();
        // }
        // this.$children[0].showMessage();
        // this.$children[0].name;

        // 2.$refs 对象类型,默认位空,引用模版的时候必须有 ref 标签,之后根据这个标签去找值
        console.log(this.$refs.aaa.name);
      },
    },
    components: {
      cpn: {
        template: "#cpn",
        data() {
          return {
            name: "子组件name"
          }
        },
        methods: {
          showMessage() {
            console.log("showMessage");
          }
        },
      }
    },
  });
</script>
</html>